//
//  SmallDecisionsBaseViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/6/14.
//

#import "SmallDecisionsBaseViewController.h"

@interface SmallDecisionsBaseViewController ()

@end

@implementation SmallDecisionsBaseViewController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    if ([self setHideNavBar]) {
        [self.navigationController.navigationBar setTranslucent:YES];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self startLoadViewData];
    if ([self setHideNavBar]) {
        [self hideSmallDecisionsNavigationBar];
    } else {
        [self showSmallDecisionsNavigationBar];
    }
    
    if ([self respondsToSelector:@selector(setLeftBarButton)]) {
        
        UIView *leftBarButton = [self setLeftBarButton];
        
        if ([leftBarButton isKindOfClass:[UIControl class]]) {
            
            [(UIControl *)leftBarButton addTarget:self action:@selector(leftBarAction:) forControlEvents:UIControlEventTouchUpInside];
        } else {
            
            for (UIView *subView in [leftBarButton subviews]) {
                
                if ([subView isKindOfClass:[UIControl class]]) {
                    
                    [(UIControl *)subView addTarget:self action:@selector(leftBarAction:) forControlEvents:UIControlEventTouchUpInside];
                    break;
                }
            }
        }
        
        UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: leftBarButton];
        self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    }
    
    if (self.navigationController.viewControllers.count > 1) {
            
        UIButton *backButton = [self setBackBarButton];
        if ([self respondsToSelector:@selector(setBackImage)]) {
            
            UIImage *backImage = [self setBackImage];
            [backButton setImage:backImage forState:UIControlStateNormal];
            [backButton setImage:backImage forState:UIControlStateHighlighted];
        }
        [backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        self.navigationItem.leftBarButtonItem = backItem;
    }
    
    if ([self respondsToSelector:@selector(setRightBarButton)]) {
        
        UIView *rightBarButton = [self setRightBarButton];
        if ([rightBarButton isKindOfClass:[UIControl class]]) {
            
            [(UIControl *)rightBarButton addTarget:self action:@selector(rightBarAction:) forControlEvents:UIControlEventTouchUpInside];
        } else {
            
            for (UIView * subView in [rightBarButton subviews]) {
                
                if ([subView isKindOfClass:[UIControl class]]) {
                    
                    [(UIControl *)subView addTarget:self action:@selector(rightBarAction:) forControlEvents:UIControlEventTouchUpInside];
                    break;
                }
            }
        }
        
        UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: rightBarButton];
        self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    }
    
    if ([self respondsToSelector:@selector(setCenterBarView)]) {
        
        UIView *centerView = [self setCenterBarView];
        if ([centerView isKindOfClass:[UIControl class]]) {
            
            [(UIControl *)centerView addTarget:self action:@selector(centerViewAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        self.navigationItem.titleView = centerView;
    }
    
    if([self respondsToSelector:@selector(showNavBarCloseButton)]) {
        BOOL showCloseButton = [self showNavBarCloseButton];
        if(showCloseButton) {
            UIView *closeBarButton = [self setCloseBarButton];
            if ([closeBarButton isKindOfClass:[UIControl class]]) {
                
                [(UIControl *)closeBarButton addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
            } else {
                
                for (UIView * subView in [closeBarButton subviews]) {
                    
                    if ([subView isKindOfClass:[UIControl class]]) {
                        
                        [(UIControl *)subView addTarget:self action:@selector(close:) forControlEvents:UIControlEventTouchUpInside];
                        break;
                    }
                }
            }
            
            UIBarButtonItem *closeBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView: closeBarButton];
            self.navigationItem.rightBarButtonItem = closeBarButtonItem;
        }
    }
    
    self.view.backgroundColor = SmallDecisionsBgColor;
}


- (void)startLoadViewData {
    [self loadRequestDataWithSuccess:^(id  _Nullable res) {
        
    } failure:^(NSError * _Nullable error) {
        
    }];
}


- (void)loadRequestDataWithSuccess:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nullable))failure {
    
}

- (UIButton *)setBackBarButton {
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0, 30, 44);
    backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [backButton setImage:[UIImage imageNamed:@"global_back"] forState:UIControlStateNormal];
    return backButton;
}

- (UIButton *)setCloseBarButton {
    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    closeButton.frame = CGRectMake(0, 0, 30, 44);
    closeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [closeButton setImage:[UIImage imageNamed:@"global_close"] forState:UIControlStateNormal];
    return closeButton;
}

- (void)leftBarAction:(UIView *)leftBar {
    
    if ([self respondsToSelector:@selector(leftBarButtonAction:)]) {
        
        [self leftBarButtonAction:leftBar];
    }
}

- (void)rightBarAction:(UIView *)rightBar {
    
    if ([self respondsToSelector:@selector(rightBarButtonAction:)]) {
        
        [self rightBarButtonAction:rightBar];
    }
}

- (void)centerViewAction:(UIView *)centerView {
    
    if ([self respondsToSelector:@selector(centerViewAction:)]) {
        
        [self centerViewAction:centerView];
    }
}

- (void)back:(UIButton *)button {
    
    if ([self respondsToSelector:@selector(backBarButtonAction:)]) {
        
        [self backBarButtonAction:button];
    } else {
        
        [SmallDecisionsNavigatorManager popViewControllerAnimated:YES];
    }
}

- (void)close:(UIButton *)button {
    
    if ([self respondsToSelector:@selector(closeBarButtonAction:)]) {
        
        [self closeBarButtonAction:button];
    } else {
        
        [SmallDecisionsNavigatorManager dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)loadRequestWithSuccess:(void (^)(id _Nullable))success failure:(void (^)(NSError * _Nullable))failure {
    
    
}

- (void)showSmallDecisionsNavigationBar {
    
    if (@available(iOS 15.0, *)) {
        
        UINavigationBarAppearance *barStandardAppearance = [UINavigationBarAppearance new];
        [barStandardAppearance configureWithOpaqueBackground];
        barStandardAppearance.shadowImage = nil;
        barStandardAppearance.shadowColor = SmallDecisionsThemeColor;
        
        barStandardAppearance.backgroundColor = SmallDecisionsNavBgColor;
        barStandardAppearance.backgroundImage = [UIImage imageWithColor:SmallDecisionsNavBgColor];
        barStandardAppearance.titleTextAttributes = @{ NSForegroundColorAttributeName : SmallDecisionsThemeFontColor, NSFontAttributeName: [UIFont systemFontOfSize:17 weight:UIFontWeightMedium]};
        
        UINavigationBarAppearance *barScrollEdgeAppearance = [UINavigationBarAppearance new];
        [barScrollEdgeAppearance configureWithOpaqueBackground];
        barScrollEdgeAppearance.shadowImage = [UIImage new];
        barScrollEdgeAppearance.shadowColor = [UIColor clearColor];
        
        barScrollEdgeAppearance.backgroundColor = SmallDecisionsNavBgColor;
        barScrollEdgeAppearance.backgroundImage = [UIImage imageWithColor:SmallDecisionsNavBgColor];
        barScrollEdgeAppearance.titleTextAttributes = @{ NSForegroundColorAttributeName : SmallDecisionsThemeFontColor, NSFontAttributeName: [UIFont systemFontOfSize:17 weight:UIFontWeightMedium]};
        
        self.navigationController.navigationBar.standardAppearance   = barStandardAppearance;
        self.navigationController.navigationBar.scrollEdgeAppearance = barScrollEdgeAppearance;
        [self.navigationController.navigationBar setTranslucent:YES];
        
    } else {
        
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor: SmallDecisionsNavBgColor] forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = [UIImage imageWithColor:SmallDecisionsNavBgColor];
        [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName: SmallDecisionsThemeFontColor, NSFontAttributeName: [UIFont systemFontOfSize:17 weight:UIFontWeightMedium]}];
    }
}

- (void)hideSmallDecisionsNavigationBar {
    if (@available(iOS 15.0, *)) {
        UINavigationBarAppearance *barStandardAppearance = [UINavigationBarAppearance new];
        [barStandardAppearance configureWithOpaqueBackground];
        barStandardAppearance.shadowColor     = [UIColor clearColor];
        barStandardAppearance.shadowImage     = nil;
        barStandardAppearance.backgroundColor = [UIColor clearColor];
        barStandardAppearance.backgroundImage = [UIImage imageWithColor:[UIColor clearColor]];
        barStandardAppearance.titleTextAttributes = @{ NSForegroundColorAttributeName : SmallDecisionsThemeFontColor, NSFontAttributeName: [UIFont systemFontOfSize:17 weight:UIFontWeightMedium]};
        self.navigationController.navigationBar.standardAppearance   = barStandardAppearance;
        
        UINavigationBarAppearance *barScrollEdgeAppearance = [UINavigationBarAppearance new];
        [barScrollEdgeAppearance configureWithOpaqueBackground];
        barScrollEdgeAppearance.backgroundColor = [UIColor clearColor];
        barScrollEdgeAppearance.shadowImage = [UIImage new];
        barScrollEdgeAppearance.shadowColor = [UIColor clearColor];
        barScrollEdgeAppearance.backgroundImage = [UIImage imageWithColor:[UIColor clearColor]];
        barScrollEdgeAppearance.titleTextAttributes = @{ NSForegroundColorAttributeName : SmallDecisionsThemeFontColor, NSFontAttributeName: [UIFont systemFontOfSize:17 weight:UIFontWeightMedium]};
        self.navigationController.navigationBar.scrollEdgeAppearance = barScrollEdgeAppearance;
        [self.navigationController.navigationBar setTranslucent: YES];
        
    } else {
        
        self.navigationController.navigationBar.barTintColor = nil;
        [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        self.navigationController.navigationBar.shadowImage = [UIImage new];
        [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName: SmallDecisionsThemeFontColor, NSFontAttributeName: [UIFont systemFontOfSize:17 weight:UIFontWeightMedium]}];
    }
}

- (BOOL)setHideNavBar {
    
    return NO;
}

- (UIView *)setCenterBarView {
    
    return nil;
}


@end
